package com.rwhansen.humidor;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.SwingConstants;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RemoveCigarFromDatabase extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel txtCigarNameHere;
	private JLabel txtFromTheCigar;
	private String databaseType;

	public RemoveCigarFromDatabase(Cigar c1, final int index, final String fileName) {
		setAlwaysOnTop(true);
		String grammer1 = "";
		String grammer2 = "";
		databaseType = "";
		setTitle("Remove " + c1.getName() + "?");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 153);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		if(fileName == Database.HUMIDOR_DB_FILENAME) {
			grammer1 = " a";
			grammer2 = " your ";
			databaseType = "humidor";
		} else if (fileName == Database.CIGAR_DB_FILENAME) {
			grammer1 = "";
			grammer2 = " the ";
			databaseType = "cigars";
		} else if (fileName == Database.RATINGS_DB_FILENAME) {
			grammer1 = " this";
			grammer2 = " your ";
			databaseType = "ratings";
		}
		
		
		JLabel lblAreYouSure = new JLabel("Are you sure you want to remove" + grammer1);
		lblAreYouSure.setHorizontalAlignment(SwingConstants.CENTER);
		lblAreYouSure.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		lblAreYouSure.setBounds(6, 10, 438, 14);
		contentPane.add(lblAreYouSure);
		
		txtCigarNameHere = new JLabel(c1.getName());
		txtCigarNameHere.setHorizontalAlignment(SwingConstants.CENTER);
		txtCigarNameHere.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		txtCigarNameHere.setBounds(6, 34, 438, 20);
		contentPane.add(txtCigarNameHere);
		
		txtFromTheCigar = new JLabel();
		txtFromTheCigar.setHorizontalAlignment(SwingConstants.CENTER);
		txtFromTheCigar.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		txtFromTheCigar.setText("from" + grammer2 + Database.getDatabaseName(fileName) + "?");
		txtFromTheCigar.setBounds(6, 64, 438, 20);
		contentPane.add(txtFromTheCigar);
		
		
		
//No Button		
		JButton btnNo = new JButton("No");
		btnNo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				PersonalCigarAssistant.setWindowEnabled(true);
			}
		});
		btnNo.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		btnNo.setBounds(0, 94, 224, 23);
		contentPane.add(btnNo);

//Yes Button		
		JButton btnYes = new JButton("Yes");
		btnYes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (databaseType == "humidor") {
					Database.removeOneCigar(index, fileName);
					PersonalCigarAssistant.refreshTables();
					PersonalCigarAssistant.setWindowEnabled(true);
					PersonalCigarAssistant.tabbedPane.setSelectedComponent(PersonalCigarAssistant.humidorScrollPane);
					PersonalCigarAssistant.humidorTable.changeSelection(0, 0, false, false);
					
				} else if (databaseType == "cigars") {
					Database.deleteCigar(index, fileName);
					PersonalCigarAssistant.refreshTables();
					PersonalCigarAssistant.setWindowEnabled(true);
					PersonalCigarAssistant.tabbedPane.setSelectedComponent(PersonalCigarAssistant.cigarDatabaseScrollPane);
					PersonalCigarAssistant.cigarDatabaseTable.changeSelection(0, 0, false, false);
					
				} else if (databaseType == "ratings") {
					Database.deleteCigar(index, fileName);
					PersonalCigarAssistant.refreshTables();
					PersonalCigarAssistant.setWindowEnabled(true);
					PersonalCigarAssistant.tabbedPane.setSelectedComponent(PersonalCigarAssistant.ratingsScrollPane);
					PersonalCigarAssistant.ratingsTable.changeSelection(0, 0, false, false);
				}
				
				setVisible(false);
				
			}
		});
		btnYes.setFont(new Font("GodOfWar", Font.PLAIN, 17));
		btnYes.setBounds(224, 94, 224, 23);
		contentPane.add(btnYes);
	}
}